<?php

class R_n_d_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = "mis_r_n_d";
        $this->load->model("functions_model");
    }

    function newR_n_d($added_by, $path) {
        $data = $_POST;
        $data['report'] = $path;
        $data['added_by'] = $added_by;
        $this->db->insert($this->tableName, $data);
    }

    function update() {
        $where['tid'] = $_POST["tid"];
        $data = $_POST;
        unset($data['tid']);
        $this->db->where($where);
        $this->db->update($this->tableName, $data);
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }

    function getOne($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->from($this->tableName);
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $row['report'] = "<a href=" . base_url() . "uploads/" . $row['report'] . ">Download</a>";
            $row['start_date'] = $this->functions_model->prettyDate($row['start_date']);
            $row['end_date'] = $this->functions_model->prettyDate($row['end_date']);
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            $result[] = $row;
        }
        return $result;
    }

    function adminGetAll($field=NULL, $search=NULL) {
        if ($field and $search) {
            if ($field == 'all') {
                $this->db->like("title", $search);
                $this->db->or_like("target", $search);
                $this->db->or_like("tools", $search);
                $this->db->or_like("added_by", $search);
            } else {
                $data[$field] = $search;
                $this->db->like($data);
            }
        }
        $this->db->select("tid,date_added, title,target, cost, report");
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);
            $row['report'] = "<a href=" . base_url() . "uploads/" . $row['report'] . ">Download</a>";
//            $row['start_date'] = $this->functions_model->prettyDate($row['start_date']);
//            $row['end_date'] = $this->functions_model->prettyDate($row['end_date']);
            $row['more'] = "<a href='javascript:loadUrl(\"#r_n_d_result\",\"" . base_url() . "index.php?/more_info/r_n_dMoreInfo/" . $row['tid'] . "\")'>More details</a>";
            if ($this->session->userdata("admin") != FALSE) {
                $row['more'].=" <a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_mis/deleteR_n_d/$tid\",\"Are you sure to delete this record?\")'>Delete</a> ";
            }
            unset($row['tid']);
            $result[] = $row;
        }
        return $result;
    }

}

?>
